package com.greensam.pixelengine.controller;

import com.greensam.pixelengine.constant.SpaceUserPermissionConstant;
import com.greensam.pixelengine.facade.SpaceUserFacadeService;
import com.greensam.pixelengine.facade.UserFacadeService;
import com.greensam.pixelengine.manager.auth.annotation.SaSpaceCheckPermission;
import com.greensam.pixelengine.pojo.base.BaseResponse;
import com.greensam.pixelengine.pojo.base.DeleteRequest;
import com.greensam.pixelengine.pojo.base.PaginationResponse;
import com.greensam.pixelengine.pojo.entity.UserPo;
import com.greensam.pixelengine.pojo.vo.spaceuser.request.SpaceUserCreateRequest;
import com.greensam.pixelengine.pojo.vo.spaceuser.request.SpaceUserEditRequest;
import com.greensam.pixelengine.pojo.vo.spaceuser.request.SpaceUserPageRequest;
import com.greensam.pixelengine.pojo.vo.spaceuser.response.SpaceUserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Ma Chengrui
 * @since 2025/9/23 9:36
 */
@Tag(name = "SpaceUserController", description = "空间用户模块")
@RestController
@RequestMapping("/space/user")
@RequiredArgsConstructor
public class SpaceUserController {

    private final UserFacadeService userFacadeService;
    private final SpaceUserFacadeService spaceUserFacadeService;

    @Operation(summary = "创建空间用户")
    @PostMapping("/create")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Long> createSpaceUser(HttpServletRequest httpServletRequest,
                                              @RequestBody SpaceUserCreateRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(spaceUserFacadeService.createSpaceUser(request, loginUser.getId()));
    }

    @Operation(summary = "删除空间用户")
    @PostMapping("/delete")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Boolean> deleteSpaceUser(HttpServletRequest httpServletRequest,
                                                 @RequestBody @Valid DeleteRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(spaceUserFacadeService.deleteSpaceUser(request, loginUser.getId()));
    }

    @Operation(summary = "编辑空间用户")
    @PostMapping("/edit")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Boolean> editSpaceUser(HttpServletRequest httpServletRequest,
                                               @RequestBody @Valid SpaceUserEditRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(spaceUserFacadeService.editSpaceUser(request, loginUser.getId()));
    }

    @Operation(summary = "获取空间用户详情")
    @GetMapping("/get")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<SpaceUserVo> getSpaceUser(@RequestParam Long id) {
        return BaseResponse.success(spaceUserFacadeService.getSpaceUser(id));
    }

    @Operation(summary = "分页查询空间用户")
    @PostMapping("/page")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<PaginationResponse<SpaceUserVo>> pageSpaceUser(@RequestBody @Valid SpaceUserPageRequest request) {
        return BaseResponse.success(spaceUserFacadeService.pageSpaceUser(request));
    }

    @Operation(summary = "获取用户加入的团队空间列表")
    @PostMapping("/my/team/list")
    public BaseResponse<List<SpaceUserVo>> listMyTeamSpace(HttpServletRequest request){
        UserPo loginUser = userFacadeService.getLoginUser(request);
        return BaseResponse.success(spaceUserFacadeService.listMyTeamSpace(loginUser.getId()));
    }
}
